package com.chris.libs.base.x.mvpp.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.io.Serializable;
import java.util.Map;

/**
 * @author Chris Chan
 * on 2019/7/9 16:54
 * use for:
 */
public abstract class BaseFragment extends Fragment {
    protected Context context;
    protected View contentView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.contentView = inflater.inflate(layoutId(), container, false);
        return this.contentView;
    }

    /**
     * 提供根布局资源ID
     * @return
     */
    protected abstract @LayoutRes
    int layoutId();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        preViewCreate(view, savedInstanceState);
        super.onViewCreated(view, savedInstanceState);
        postViewCreated(view, savedInstanceState);
    }

    protected abstract void preViewCreate(View view, Bundle savedInstanceState);

    protected abstract void postViewCreated(View view, Bundle savedInstanceState);

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    public View getContentView() {
        return contentView;
    }

    public Context getBaseContext() {
        return this.context;
    }

    /**
     * 跳转到其他的Activity
     *
     * @param clazz
     */
    protected void startActivity(Class<?> clazz) {
        Intent intent = new Intent(getContext(), clazz);
        startActivity(intent);
        afterStartActivity();
    }

    protected void startActivityAndFinished(Class<?> clazz) {
        Intent intent = new Intent(getContext(), clazz);
        startActivity(intent);
        afterStartActivity();
        finishActivity();
    }

    protected void startActivityWithExtra(Class<?> clazz, String key, Serializable value) {
        Intent intent = new Intent(getContext(), clazz);
        intent.putExtra(key, value);
        startActivity(intent);
        afterStartActivity();
    }

    protected void startActivityWithExtraMap(Class<?> clazz, Map<String, Serializable> map) {
        Intent intent = new Intent(getContext(), clazz);
        for (Map.Entry<String, Serializable> entry : map.entrySet()) {
            intent.putExtra(entry.getKey(), entry.getValue());
        }
        startActivity(intent);
        afterStartActivity();
    }

    protected void startActivityForResult(Class<?> clazz, int requestCode) {
        startActivityForResult(new Intent(getContext(), clazz), requestCode);
        afterStartActivity();
    }


    /**
     * 结束Activity
     */
    protected void finishActivity() {
        if (this.context instanceof Activity) {
            ((Activity) this.context).finish();
        }
    }

    /**
     * 在开启另一个页面之后紧接着执行
     * 初衷是为了自定义页面跳转动画
     */
    protected abstract void afterStartActivity();
}
